package com.adingxiong.quickstart.project.controller;

import com.adingxiong.quickstart.project.entity.EmailLog;
import com.adingxiong.quickstart.project.entity.vo.EchartVo;
import com.adingxiong.quickstart.project.entity.vo.GroupByEamil;
import com.adingxiong.quickstart.project.entity.vo.HomeCountVo;
import com.adingxiong.quickstart.project.service.EchartService;
import com.adingxiong.quickstart.project.service.IEmailLogService;
import com.adingxiong.quickstart.result.Result;
import com.adingxiong.quickstart.result.RetCode;
import com.adingxiong.quickstart.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import liquibase.pro.packaged.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName EchartController
 * @Description 首页统计图所需
 * @Author xiongchao
 * @Date 2020/6/11 10:46
 **/
@RestController
@Api(tags = "首页统计图数据")
@RequestMapping("/home")
public class EchartController {


    @Autowired
    private EchartService service;

    @Autowired
    private IEmailLogService logService;

    @GetMapping("/views")
    @ApiOperation(value = "月度网站访问人数记录")
    public Result<List<EchartVo>> netCountGroupByDate(
            @ApiParam(value = "开始时间") @RequestParam(value = "startDate",required = false) String startDate,
            @ApiParam(value = "结束时间") @RequestParam(value = "endDate" ,required = false) String endDate
    ){
        List<EchartVo> res = service.countGroupByDate(startDate,endDate);

        return new Result(RetCode.SUCCESS.getCode(),RetCode.SUCCESS.getMsg(),res);
    }

    @GetMapping("/total")
    @ApiOperation(value = "获取总数")
    public Result<HomeCountVo> getTotalCount(){
        HomeCountVo res = service.getTotal();
        return new Result(RetCode.SUCCESS.getCode(),RetCode.SUCCESS.getMsg(),res);
    }


    @GetMapping("/emailAnaly")
    @ApiOperation(value = "按条件统计邮箱日志")
    public Result<List<GroupByEamil>> eamilGroupByDate(
         @ApiParam(value = "统计类型 year,month",defaultValue = "year") @RequestParam(value = "type" ,required = false,defaultValue = "year") String type
    ){
        List<GroupByEamil> res = new ArrayList<>();
        if(type.equals("year")){
            List<String> months = DateUtils.getMonths();
            for(String str : months){
                GroupByEamil eamil = new GroupByEamil();
                QueryWrapper<EmailLog> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("DATE_FORMAT(send_time,'%Y-%m')",str);
                Integer count = logService.count(queryWrapper);
                eamil.setCount(count);
                eamil.setDate(str);

                res.add(eamil);
            }
        }
        if(type.equals("month")){
            List<String> days = DateUtils.getDays();
            for(String str : days){
                GroupByEamil eamil = new GroupByEamil();
                QueryWrapper<EmailLog> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("DATE_FORMAT(send_time,'%Y-%m-%d')",str);
                Integer count = logService.count(queryWrapper);
                eamil.setCount(count);
                eamil.setDate(str);
                res.add(eamil);
            }
        }

        return new Result(RetCode.SUCCESS.getCode(),RetCode.SUCCESS.getMsg(),res);
    }


}
